/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.iecg.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iecg.api.entity.IecgCheckCondition;
import com.iecg.api.entity.IecgUser;
import com.iecg.dao.IecgCheckConditionRepository;
import com.iecg.service.IecgCheckConditionService;
import com.iecg.service.IecgUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

;

/**
 *
 *
 * @author zjc
 * @date 2019-09-05 11:07:08
 */
@Service
public class IecgCheckConditionServiceImpl implements IecgCheckConditionService {

	@Autowired
	private IecgCheckConditionRepository checkConditionRepository;

	@Autowired
	private IecgUserService iecgUserService;

	@Autowired
	private MongoTemplate mongoTemplate;

	public IecgCheckCondition save(IecgCheckCondition iecgCheckCondition) {
		IecgUser iecgUser = iecgUserService.getSecurityUser();
		iecgCheckCondition.setUserId(iecgUser.getId());
		iecgCheckCondition.setId(null);
		return checkConditionRepository.save(iecgCheckCondition);
	}

	public Optional<IecgCheckCondition> findById(String id) {
		return checkConditionRepository.findById(id);
	}

	@Override
	public Page<IecgCheckCondition> page(Pageable pageable, IecgCheckCondition iecgCheckCondition) {

		Query query = new Query();

		//动态拼接查询条件
//		if (!StringUtils.isEmpty(studentReqVO.getName())){
//			Pattern pattern = Pattern.compile("^.*" + studentReqVO.getName() + ".*$", Pattern.CASE_INSENSITIVE);
//			query.addCriteria(Criteria.where("name").regex(pattern));
//		}

		if (iecgCheckCondition.getUserId()!=null){
			query.addCriteria(Criteria.where("bizId").is(iecgCheckCondition.getUserId()));
		}

		//计算总数
		long total = mongoTemplate.count(query, IecgCheckCondition.class);

		//查询结果集
		List<IecgCheckCondition> resultList = mongoTemplate.find(query.with(pageable), IecgCheckCondition.class);
		Page<IecgCheckCondition> pages = new Page(pageable.getPageNumber(), pageable.getPageSize(), total);
		pages.setRecords(resultList);
		pages.setSearchCount(true);
		return pages;
	}

	public IecgCheckCondition modifyCheckCondition(IecgCheckCondition checkCondition) {
		return checkConditionRepository.save(checkCondition);
	}

	public void deleteCheckCondition(String id) {
		checkConditionRepository.deleteById(id);
	}

}
